package com.huadao.cart_ccs.experiment.log.operationRecord;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RadioGroup;
import android.widget.Spinner;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseFragment;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.experiment.log.OnDay;
import com.huadao.cart_ccs.experiment.log.baseLoglist.BaseLogListFrag;
import com.huadao.cart_ccs.experiment.log.deviceLog.DeviceRecordFrag;
import com.huadao.cart_ccs.experiment.log.environmentLog.EnvironmentLogFrag;
import com.huadao.cart_ccs.experiment.log.operationLog.HumanOperateLogFrag;

import butterknife.BindView;

/**
 * 操作记录,包含了 设备记录，环境记录，流程记录
 */
public class OperationRecordFrag extends BaseFragment implements OnDay{
    @BindView(R.id.sp_condition_device_record)
    Spinner sp_condition_device_record;
    @BindView(R.id.sp_condition_environment_record)
    Spinner sp_condition_environment_record;
    @BindView(R.id.sp_condition_operate_record)
    Spinner sp_condition_operate_record;

    @BindView(R.id.rg_operateRecord)
    RadioGroup rg_operateRecord;

    private FragmentManager fragmentManager;
    private BaseLogListFrag currentFrag;
    private int selectedDay;

    private EnvironmentLogFrag environmentLogFrag;
    private HumanOperateLogFrag humanOperateLogFrag;
    private DeviceRecordFrag deviceRecordFrag;

    public static OperationRecordFrag getInstance(int day){
        Bundle bundle=new Bundle();
        bundle.putInt("day",day);
        OperationRecordFrag operationRecordFrag=new OperationRecordFrag();
        operationRecordFrag.setArguments(bundle);
        return operationRecordFrag;
    }

    public void setDayBundle(int day){
        Bundle bundle=new Bundle();
        bundle.putInt("day",day);
        this.setArguments(bundle);
    }

    @Override
    public int getContentViewId() {
        return R.layout.frag_operation_record;
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        fragmentManager=getChildFragmentManager();
        Bundle bundle=getArguments();
        if(bundle!=null&&bundle.containsKey("day")){
            int day=bundle.getInt("day");
            selectedDay=day;
        }

        rg_operateRecord.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==R.id.rb_deviceRecord){
                    if(deviceRecordFrag==null){
                        deviceRecordFrag=DeviceRecordFrag.getInstance(selectedDay, (String) sp_condition_device_record.getSelectedItem());
                    }else {
                        deviceRecordFrag.setDayConditionBundle(selectedDay, (String) sp_condition_device_record.getSelectedItem());
                    }
                    replaceFrag(deviceRecordFrag);
                    setCondition(1);
                }else if(checkedId==R.id.rb_environmentRecord){
                    if(environmentLogFrag==null){
                        environmentLogFrag=EnvironmentLogFrag.getInstance(selectedDay, (String) sp_condition_environment_record.getSelectedItem());
                    }else {
                        environmentLogFrag.setDayConditionBundle(selectedDay, (String) sp_condition_environment_record.getSelectedItem());
                    }
                    replaceFrag(environmentLogFrag);
                    setCondition(2);
                }else if(checkedId==R.id.rb_operateRecord){
                    if(humanOperateLogFrag==null){
                        humanOperateLogFrag=HumanOperateLogFrag.getInstance(selectedDay, (String) sp_condition_operate_record.getSelectedItem());
                    }else {
                        humanOperateLogFrag.setDayConditionBundle(selectedDay, (String) sp_condition_operate_record.getSelectedItem());
                    }
                    replaceFrag(humanOperateLogFrag);
                    setCondition(3);
                }
            }
        });

        ConditionListener conditionListener=new ConditionListener();
        sp_condition_device_record.setOnItemSelectedListener(conditionListener);
        sp_condition_environment_record.setOnItemSelectedListener(conditionListener);
        sp_condition_operate_record.setOnItemSelectedListener(conditionListener);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

//    @OnClick({R.id.deviceRecord, R.id.btn_environmentRecord, R.id.btn_operateRecord})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.deviceRecord:
//                if(deviceRecordFrag==null){
//                    deviceRecordFrag=DeviceRecordFrag.getInstance(selectedDay, (String) sp_condition_device_record.getSelectedItem());
//                }
//                replaceFrag(deviceRecordFrag);
//                setCondition(1);
//                break;
//            case R.id.btn_environmentRecord:
//                if(environmentLogFrag==null){
//                    environmentLogFrag=EnvironmentLogFrag.getInstance(selectedDay, (String) sp_condition_environment_record.getSelectedItem());
//                }
//                replaceFrag(environmentLogFrag);
//                setCondition(2);
//                break;
//            case R.id.btn_operateRecord:
//                if(humanOperateLogFrag==null){
//                    humanOperateLogFrag=HumanOperateLogFrag.getInstance(selectedDay, (String) sp_condition_operate_record.getSelectedItem());
//                }
//                replaceFrag(humanOperateLogFrag);
//                setCondition(3);
//                break;
//        }
//    }

    private void replaceFrag(Fragment fragment) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fl_log, fragment).commitAllowingStateLoss();
        currentFrag= (BaseLogListFrag) fragment;
    }

    @Override
    public void daySelected(int day) {
        selectedDay=day;
        if(currentFrag!=null) {
            currentFrag.daySelected(selectedDay);
        }
    }

    private void setCondition(int number){
        if(number==1){
            sp_condition_device_record.setVisibility(View.VISIBLE);
            sp_condition_environment_record.setVisibility(View.GONE);
            sp_condition_operate_record.setVisibility(View.GONE);
        }else if(number==2){
            sp_condition_device_record.setVisibility(View.GONE);
            sp_condition_environment_record.setVisibility(View.VISIBLE);
            sp_condition_operate_record.setVisibility(View.GONE);
        }else if(number==3){
            sp_condition_device_record.setVisibility(View.GONE);
            sp_condition_environment_record.setVisibility(View.GONE);
            sp_condition_operate_record.setVisibility(View.VISIBLE);
        }
    }

    class ConditionListener implements AdapterView.OnItemSelectedListener {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String condition= (String) parent.getItemAtPosition(position);
            if(currentFrag!=null) {
                currentFrag.conditionSelected(condition);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    }
}
